home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / tfog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.9 KB  |  430 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <X11/keysym.h>
  21.  
  22. static int RGBattributes[] = {
  23.     GLX_RGBA,
  24.     GLX_RED_SIZE, 1,
  25.     GLX_GREEN_SIZE, 1,
  26.     GLX_BLUE_SIZE, 1,
  27.     GLX_DEPTH_SIZE, 1,
  28.     None,
  29. };
  30.  
  31. static int CIattributes[] = {
  32.     GLX_DEPTH_SIZE, 1,
  33.     None,
  34. };
  35.  
  36. enum {
  37.     BLACK = 0,
  38.     RED,
  39.     GREEN,
  40.     YELLOW,
  41.     BLUE,
  42.     MAGENTA,
  43.     CYAN,
  44.     WHITE
  45. };
  46.  
  47. #define COLOR_OFFSET_1    16
  48. #define COLOR_OFFSET_2    32
  49.  
  50.  
  51. static float rgbMap[][3] = {
  52.     {0, 0, 0},
  53.     {1, 0, 0},
  54.     {0, 1, 0},
  55.     {1, 1, 0},
  56.     {0, 0, 1},
  57.     {1, 0, 1},
  58.     {0, 1, 1},
  59.     {1, 1, 1}
  60. };
  61.  
  62. static long W = 300, H = 300;
  63. static long size, mode, rgb;
  64. static long antiAlias = 0;
  65. static long stipple = 0;
  66. static long stencil = 0;
  67. static long fog = 0;
  68. static long smoothShade = 1;
  69. static long feedback = 0;
  70. static long ci1 = BLUE, ci2 = GREEN;
  71. static unsigned char stippleBits[32*4] = {
  72.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  73.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  74.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  75.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  76.  
  77.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  78.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  79.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  80.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  81.  
  82.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  83.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  84.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  85.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  86.  
  87.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  88.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  89.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  90.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  91. };
  92.  
  93.  
  94. static void DoDisplay(void)
  95. {
  96.     GLint i, n;
  97.     GLfloat buf[20000];
  98.  
  99.  
  100.     glMatrixMode(GL_PROJECTION);
  101.     glLoadIdentity();
  102.     glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
  103.     glMatrixMode(GL_MODELVIEW);
  104.  
  105.     glClearColor(0.0, 0.0, 0.0, 0.0);
  106.     glClearStencil(0);
  107.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  108.  
  109.     glColor3f(1.0, 0.0, 0.0);
  110.  
  111.     if (fog) {
  112.     glEnable(GL_FOG);
  113.     glFogi(GL_FOG_MODE, GL_LINEAR);
  114.     } else {
  115.     glDisable(GL_FOG);
  116.     }
  117.  
  118.     if (smoothShade) {
  119.     glShadeModel(GL_SMOOTH);
  120.     } else {
  121.     glShadeModel(GL_FLAT);
  122.     }
  123.  
  124.     if (antiAlias) {
  125.     glEnable(GL_POLYGON_SMOOTH);
  126.     glEnable(GL_BLEND);
  127.     glDisable(GL_DEPTH_TEST);
  128.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  129.     } else {
  130.     glDisable(GL_POLYGON_SMOOTH);
  131.     glDisable(GL_BLEND);
  132.     }
  133.     if (stipple) {
  134.     glPolygonStipple(stippleBits);
  135.     glEnable(GL_POLYGON_STIPPLE);
  136.     } else {
  137.     glDisable(GL_POLYGON_STIPPLE);
  138.     }
  139.  
  140.     if (stencil) {
  141.     glEnable(GL_STENCIL_TEST);
  142.     glStencilOp(GL_KEEP, GL_ZERO, GL_INCR); 
  143.     } else {
  144.     glDisable(GL_STENCIL_TEST);
  145.     }
  146.  
  147.  
  148.     glBegin(GL_POINTS);
  149.     for (i = 0; i < 20; i++) {
  150.     glVertex3f(10+i*5, 95, i*0.1);
  151.     }
  152.     glEnd();
  153.  
  154.     glBegin(GL_LINE_STRIP);
  155.     for (i = 0; i < 9; i++) {
  156.     glVertex3f(10+i*10, 90, i*0.1);
  157.     }
  158.     glEnd();
  159.  
  160.     if (feedback) {
  161.     glFeedbackBuffer(10000, GL_3D_COLOR, buf);
  162.     glRenderMode(GL_FEEDBACK);
  163.     }
  164.     for (i = 0; i < 8; i++) {
  165.     glBegin(GL_POLYGON);
  166.     glColor3f(1.0, 0.0, 0.0);
  167.     glVertex3f(10+i*10, 85, i*0.1);
  168.     glVertex3f(10+i*10, 75, i*0.1);
  169.     glColor3f(0.0, 0.0, 1.0);
  170.     glVertex3f(20+i*10, 75, i*0.1);
  171.     glVertex3f(20+i*10, 85, i*0.1);
  172.     glEnd();
  173.     }
  174.     if (feedback) {
  175.     n = glRenderMode(GL_RENDER);
  176.     i = 2;
  177.     printf("beginning %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
  178.     i = 9;
  179.     printf("          %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
  180.     i = n-14;
  181.     printf("end       %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
  182.     i = n-7;
  183.     printf("          %f %f %f %f %f %f %f\n", buf[i+0], buf[i+1], buf[i+2],buf[i+3], buf[i+4], buf[i+5], buf[i+6]);
  184.     }
  185.     
  186.     glRectf(10.0, 68.0, 90.0, 73.0);
  187.  
  188.     glBegin(GL_LINE_LOOP);
  189.     glColor3f(1.0, 0.0, 0.0);
  190.     for (i = 0; i < 8; i++) {
  191.     glVertex3f(10+i*10, 60-i, i*0.1);
  192.     glColor3f(0.0, 0.0, 1.0);
  193.     }
  194.     glVertex3f(80, 65, 0.8);
  195.     glEnd();
  196.  
  197.  
  198.     glBegin(GL_TRIANGLES);
  199.     for (i = 0; i < 9; i++) {
  200.     glColor3f(1.0, 0.0, 0.0);
  201.     glVertex3f(10+i*10, 45, i*0.1);
  202.     glVertex3f(10+i*10, 35, i*0.1);
  203.     glColor3f(0.0, 0.0, 1.0);
  204.     glVertex3f(20+i*10, 35, i*0.1);
  205.     glVertex3f(20+i*10, 45, i*0.1);
  206.     }
  207.     glEnd();
  208.  
  209.     glBegin(GL_TRIANGLE_STRIP);
  210.     for (i = 0; i < 9; i++) {
  211.     glColor3f(0.0, 1.0, 0.0);
  212.     glVertex3f(10+i*10, 30, i*0.1);
  213.     glColor3f(0.0, 0.0, 1.0);
  214.     glVertex3f(10+i*10, 20, i*0.1);
  215.     }
  216.     glEnd();
  217.  
  218.     glPolygonMode(GL_FRONT, GL_LINE);
  219.     glBegin(GL_TRIANGLE_STRIP);
  220.     for (i = 0; i < 9; i++) {
  221.     glColor3f(0.0, 1.0, 0.0);
  222.     glVertex3f(10+i*10, 15, i*0.1);
  223.     glColor3f(0.0, 0.0, 1.0);
  224.     glVertex3f(10+i*10, 10, i*0.1);
  225.     }
  226.     glEnd();
  227.  
  228.     glPolygonMode(GL_FRONT, GL_POINT);
  229.     glBegin(GL_TRIANGLE_STRIP);
  230.     for (i = 0; i < 9; i++) {
  231.     glColor3f(0.0, 1.0, 0.0);
  232.     glVertex3f(10+i*10, 5, i*0.1);
  233.     glColor3f(0.0, 0.0, 1.0);
  234.     glVertex3f(10+i*10, 1, i*0.1);
  235.     }
  236.     glEnd();
  237.     glPolygonMode(GL_FRONT, GL_FILL);
  238.  
  239.     glFlush();
  240. }
  241.  
  242. static void Usage(void)
  243. {
  244.     printf("Usage: tdepth [-c]\n");
  245.     printf("   -c:  Run in color index mode\n");
  246.     exit(-1);
  247. }
  248.  
  249. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  250. {
  251.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  252.     return GL_TRUE;
  253.     }
  254.     return GL_FALSE;
  255. }
  256.  
  257. int main(long argc, char** argv)
  258. {
  259.     XVisualInfo *vi;
  260.     Display *dpy;
  261.     Colormap cmap;
  262.     Window window;
  263.     XSetWindowAttributes swa;
  264.     GLXContext cx;
  265.     XEvent event;
  266.     GLboolean needDisplay;
  267.     int i;
  268.  
  269.     rgb = 1;
  270.     for (i = 1; i < argc; i++) {
  271.         if (argv[i][0] == '-') {
  272.             switch (argv[i][1]) {
  273.               case 'c':
  274.                 rgb = 0;
  275.                 break;
  276.               default:
  277.                 Usage();
  278.             }
  279.         } else {
  280.             Usage();
  281.         }
  282.     }
  283.  
  284.     dpy = XOpenDisplay(0);
  285.     if (!dpy) {
  286.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  287.     return -1;
  288.     }
  289.  
  290.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  291.              rgb ? RGBattributes : CIattributes);
  292.     if (!vi) {
  293.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  294.         getenv("DISPLAY"));
  295.     return -1;
  296.     }
  297.  
  298.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  299.                rgb ? AllocNone : AllocAll);
  300.     swa.border_pixel = 0;
  301.     swa.colormap = cmap;
  302.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  303.     | KeyReleaseMask;
  304.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  305.                W, H,
  306.                0, vi->depth, InputOutput, vi->visual,
  307.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  308.     XSetWMColormapWindows(dpy, window, &window, 1);
  309.     XMapWindow(dpy, window);
  310.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  311.  
  312.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  313.     if (!glXMakeCurrent(dpy, window, cx)) {
  314.     fprintf(stderr, "Can't make window current to context\n");
  315.     return -1;
  316.     }
  317.  
  318.     if (!rgb) {
  319.     XColor xc[32+2];
  320.  
  321.     /* Build two color ramps to use */
  322.     unsigned long pixel;
  323.     unsigned short red, green, blue;
  324.     char flags;  /* do_red, do_green, do_blue */
  325.     char pad;
  326.  
  327.     for (i = 0; i < 16; i++) {
  328.         xc[i].pixel = i + COLOR_OFFSET_1;
  329.         xc[i].red = 0;
  330.         xc[i].green = 0;
  331.         xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
  332.         xc[i].flags = DoRed | DoGreen | DoBlue;
  333.  
  334.         xc[i+16].pixel = i + COLOR_OFFSET_2;
  335.         xc[i+16].red = 0;
  336.         xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
  337.         xc[i+16].blue = 0;
  338.         xc[i+16].flags = DoRed | DoGreen | DoBlue;
  339.     }
  340.  
  341.     xc[32].pixel = BLUE;
  342.     xc[32].red = xc[32].green = 0;
  343.     xc[32].blue = 65535;
  344.     xc[32].flags = DoRed|DoGreen|DoBlue;
  345.  
  346.     xc[32].pixel = GREEN;
  347.     xc[32].red = xc[32].blue = 0;
  348.     xc[32].green = 65535;
  349.     xc[32].flags = DoRed|DoGreen|DoBlue;
  350.     XStoreColors(dpy, cmap, xc, 34);
  351.     }
  352.  
  353.     mode = 0;
  354.     size = 1;
  355.  
  356.     needDisplay = GL_TRUE;
  357.     for (;;) {
  358.     do {
  359.         XNextEvent(dpy, &event);
  360.         switch (event.type) {
  361.           case Expose:
  362.         needDisplay = GL_TRUE;
  363.         break;
  364.           case ConfigureNotify:
  365.         W = event.xconfigure.width;
  366.         H = event.xconfigure.height;
  367.         needDisplay = GL_TRUE;
  368.         break;
  369.           case KeyPress:
  370.         {
  371.             char buf[100];
  372.             int rv;
  373.             KeySym ks;
  374.  
  375.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  376.             switch (ks) {
  377.               case XK_A:
  378.               case XK_a:
  379.             antiAlias = !antiAlias;
  380.             if (!rgb) {
  381.                 if (antiAlias) {
  382.                 ci1 = COLOR_OFFSET_1;
  383.                 ci2 = COLOR_OFFSET_2;
  384.                 } else {
  385.                 ci1 = BLUE;
  386.                 ci2 = GREEN;
  387.                 }
  388.             }
  389.             needDisplay = GL_TRUE;
  390.             break;
  391.               case XK_E:
  392.               case XK_e:
  393.             feedback = !feedback;
  394.             needDisplay = GL_TRUE;
  395.             break;
  396.               case XK_F:
  397.               case XK_f:
  398.             fog = !fog;
  399.             needDisplay = GL_TRUE;
  400.             break;
  401.               case XK_S:
  402.               case XK_s:
  403.             stipple = !stipple;
  404.             needDisplay = GL_TRUE;
  405.             break;
  406.               case XK_T:
  407.               case XK_t:
  408.             stencil = !stencil;
  409.             needDisplay = GL_TRUE;
  410.             break;
  411.               case XK_M:
  412.               case XK_m:
  413.             smoothShade = !smoothShade;
  414.             needDisplay = GL_TRUE;
  415.             break;
  416.               case XK_Escape:
  417.             return 0;
  418.             }
  419.         }
  420.         break;
  421.         }
  422.     } while (XPending(dpy) != 0);
  423.  
  424.     if (needDisplay) {
  425.         needDisplay = GL_FALSE;
  426.         DoDisplay();
  427.     }
  428.     }
  429. }
  430.